import {createRouter, createWebHashHistory, RouteLocationRaw, RouteRecordRaw} from "vue-router";
import {RouterName} from "@/utils/enum-type";

export const Layout = () => import("@/layout/index.vue");

// 静态路由
export const constantRoutes: RouteRecordRaw[] = [
    {
        path: "/",
        component: Layout,
        redirect: "/dashboard",
        name: RouterName.Layout,
        children: [
            {
                path: "dashboard",
                component: () => import("@/views/dashboard/index.vue"),
                name: RouterName.Dashboard, // 用于 keep-alive, 必须与SFC自动推导或者显示声明的组件name一致
                meta: {
                    title: "首页", // 标题必填
                    icon: "homepage", // 按钮
                    affix: true,  // 是否固定(没实现)
                    keepAlive: true, // 没实现
                    white: true, // 是否为白名单
                },
            },
            {
                path: "/login",
                component: () => import("@/views/login/index.vue"),
                name: RouterName.Login,
                meta: {hidden: true, white: true},
            },
            {
                path: 'sys',
                name: 'Sys', // 这个不要改,其他地方用到了name
                redirect: '/sys/home',
                component: () => import("@/views/sys/index.vue"),
                meta: {
                    title: '设置',
                },
                children: [
                    {
                        path: 'home',
                        name: RouterName.Home,
                        component: () => import("@/views/sys/home/index.vue"),
                        meta: {
                            title: '首页',
                            white: true,
                        }
                    },
                    // 所有的动态路由都加入到这里
                ]
            },
            {
                path: "401",
                name: RouterName.Unauthorized,
                component: () => import("@/views/error-page/401.vue"),
                meta: {hidden: true},
            },
            {
                path: "404",
                name: RouterName.NotFound,
                component: () => import("@/views/error-page/404.vue"),
                meta: {hidden: true},
            },
        ],
    }, {
        path: "/qq",
        name: RouterName.QQ,
        component: () => import("@/views/qq/index.vue"),
        redirect: '/qq/home',
        children: [
            {
                path: 'home',
                name: RouterName.QQHome,
                component: () => import("@/views/qq/home/HomeView.vue"),
            },
            {
                path: 'task',
                name: RouterName.QQTask,
                component: () => import("@/views/qq/task/task-left/TaskLeftIndex.vue"),
            },
            {
                path: 'bag',
                name: RouterName.QQBag,
                component: () => import("@/views/qq/bag/bag-left/BagLeftIndex.vue"),
            },
            {
                path: 'recharge',
                name: RouterName.QQRecharge,
                component: () => import("@/views/qq/top/top-right/RechargeView.vue"),
            },
        ],

    }
];

/**
 * 创建路由
 */
let router = createRouter({
    history: createWebHashHistory(),
    routes: constantRoutes,
    // 刷新时，滚动条位置还原
    // scrollBehavior: () => ({left: 0, top: 0}),
});

/**
 * 同push
 */
export function pushTo(to: RouteLocationRaw) {
    return router.push(to)
}

/**
 * 重置路由
 */
export function resetRouter() {
    return router.replace({path: "/login"});
}

/**
 * 获取白名单
 */
export function getWhiteList(targetRoutes: RouteRecordRaw[]) {
    const whiteList: (string | symbol)[] = []
    targetRoutes.forEach(item => {
        if (item.meta?.white) {
            whiteList.push(item.name || 'Layout')
        }
        // 如果有孩子的话
        if (Array.isArray(item.children) && item.children.length > 0) {
            whiteList.push(...getWhiteList(item.children))
        }
    })
    return [...new Set(whiteList)]
}

export {router};
